from fastapi import FastAPI
from fastapi import Response

from fastapi.middleware.cors import CORSMiddleware

import uvicorn
import datetime
import numpy as np
import pandas as pd

app = FastAPI()


origins = ["*"]

app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)


@app.get("/")
async def root():
    return {"message": "Hello World", "datetime": datetime.datetime.now(), "test": 'this is a test'}


@app.get('/testapi')
async def return_simple_data():
    '''
    这个是返回的一个随机数据
    '''
    num_part = 10
    simple_data = pd.DataFrame({'partname': [f"part_{i}" for i in range(num_part)],
                                'gmv': np.random.randint(low=10, high=30, size=num_part)})

    simple_data['nmv'] = simple_data['gmv'] - \
        np.random.randint(low=0, high=3, size=num_part)
    return Response(simple_data.to_json(orient='records'), media_type='application/json')


if __name__ == '__main__':
    uvicorn.run(app='simpleapi:app',
                port=8000, reload=True, debug=True)
